.TH std::rend,std::crend 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::rend,std::crend \- std::rend,std::crend

.SH Synopsis
   Defined in header <array>
   Defined in header <deque>
   Defined in header <flat_map>
   Defined in header <flat_set>
   Defined in header <forward_list>
   Defined in header <iterator>
   Defined in header <list>
   Defined in header <map>
   Defined in header <regex>
   Defined in header <set>
   Defined in header <span>
   Defined in header <string>
   Defined in header <string_view>
   Defined in header <unordered_map>
   Defined in header <unordered_set>
   Defined in header <vector>
   template< class C >                          (since
   auto rend( C& c ) ->                         C++14)
   decltype(c.rend());                          (until
                                                C++17)
   template< class C >                          (since
   constexpr auto rend( C& c ) ->               C++17)
   decltype(c.rend());
   template< class C >                                  (since
   auto rend( const C& c ) ->                           C++14)
   decltype(c.rend());                                  (until
                                                        C++17)
   template< class C >                                  (since
   constexpr auto rend( const C& c ) ->                 C++17)
   decltype(c.rend());
   template< class T, std::size_t N >                           (since
   std::reverse_iterator<T*> rend( T                            C++14)
   (&array)[N] );                                               (until
                                        \fB(1)\fP                     C++17)
   template< class T, std::size_t N >                           (since
   constexpr std::reverse_iterator<T*>                          C++17)
   rend( T (&array)[N] );
   template< class T >                      \fB(2)\fP                         (since
   std::reverse_iterator<const T*>                                      C++14)
   rend( std::initializer_list<T> il );                                 (until
                                                \fB(3)\fP                     C++17)
   template< class T >
   constexpr                                                            (since
   std::reverse_iterator<const T*>                                      C++17)
   rend( std::initializer_list<T> il );                 \fB(4)\fP
   template< class C >                                                          (since
   auto crend( const C& c ) ->                                                  C++14)
   decltype(std::rend(c));                                                      (until
                                                                \fB(5)\fP             C++17)
   template< class C >                                                          (since
   constexpr auto crend( const C& c )                                           C++17)
   -> decltype(std::rend(c));

   Returns an iterator to the reverse-end of the given range.

   1,2) Returns c.rend(), which is typically an iterator one past the reverse-end of
   the sequence represented by c.
   1) If C is a standard Container, returns a C::reverse_iterator object.
   2) If C is a standard Container, returns a C::const_reverse_iterator object.
   3) Returns an std::reverse_iterator<T*> object to the reverse-end of array.
   4) Returns an std::reverse_iterator<const T*> object to the reverse-end of il.
   5) Returns std::end(c), with c always treated as const-qualified.
   If C is a standard Container, returns a C::const_reverse_iterator object.

   range-rbegin-rend.svg

.SH Parameters

   c     - a container or view with a rend member function
   array - an array of arbitrary type
   il    - an std::initializer_list

.SH Return value

   1,2) c.rend()
   3) std::reverse_iterator<T*>(array)
   4) std::reverse_iterator<const T*>(il.begin())
   5) c.rend()

.SH Exceptions

   May throw implementation-defined exceptions.

   Overloads

   Custom overloads of rend may be provided for classes and enumerations that do not
   expose a suitable rend() member function, yet can be iterated.

   Overloads of rend found by argument-dependent lookup can be used to    \fI(since C++20)\fP
   customize the behavior of std::ranges::rend and std::ranges::crend.

.SH Notes

   The overload for std::initializer_list is necessary because it does not have a
   member function rend.

.SH Example


// Run this code

 #include <algorithm>
 #include <iostream>
 #include <iterator>
 #include <vector>

 int main()
 {
     int a[]{4, 6, -3, 9, 10};
     std::cout << "C-style array `a` backwards: ";
     std::copy(std::rbegin(a), std::rend(a), std::ostream_iterator<int>(std::cout, " "));

     auto il = {3, 1, 4};
     std::cout << "\\nstd::initializer_list `il` backwards: ";
     std::copy(std::rbegin(il), std::rend(il), std::ostream_iterator<int>(std::cout, " "));

     std::vector<int> v{4, 6, -3, 9, 10};
     std::cout << "\\nstd::vector `v` backwards: ";
     std::copy(std::rbegin(v), std::rend(v), std::ostream_iterator<int>(std::cout, " "));
     std::cout << '\\n';
 }

.SH Output:

 C-style array `a` backwards: 10 9 -3 6 4
 std::initializer_list `il` backwards: 4 1 3
 std::vector `v` backwards: 10 9 -3 6 4

.SH See also

   end
   cend          returns an iterator to the end of a container or array
   \fI(C++11)\fP       \fI(function template)\fP
   \fI(C++14)\fP
   rbegin        returns a reverse iterator to the beginning of a container or array
   crbegin       \fI(function template)\fP
   \fI(C++14)\fP
   begin
   cbegin        returns an iterator to the beginning of a container or array
   \fI(C++11)\fP       \fI(function template)\fP
   \fI(C++14)\fP
   ranges::rend  returns a reverse end iterator to a range
   (C++20)       (customization point object)
   ranges::crend returns a reverse end iterator to a read-only range
   (C++20)       (customization point object)
